package com.api.shopapi.mapper;

import com.api.shopapi.entity.AlarmRecord;
import com.api.shopapi.entity.dto.AlarmRecordDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface AlarmRecordMapper {

    @Insert("insert into alarm_record (AlarmRecordCode," +
            "UserCode," +
            "Remarks," +
            "CreateTime," +
            "GovernmentCode," +
            "Type," +
            "DeviceName," +
            "Location) " +
            "value(#{alarmRecord.AlarmRecordCode}," +
            "#{alarmRecord.UserCode}," +
            "#{alarmRecord.Remarks}," +
            "#{alarmRecord.CreateTime}," +
            "#{alarmRecord.GovernmentCode}," +
            "#{alarmRecord.Type}," +
            "#{alarmRecord.DeviceName}," +
            "#{alarmRecord.Location})")
    public int insert(@Param("alarmRecord") AlarmRecord alarmRecord);

    @Update("update alarm_record " +
            "set " +
            "AlarmRecordCode = #{alarmRecord.AlarmRecordCode}," +
            "UserCode = #{alarmRecord.UserCode}," +
            "Remarks = #{alarmRecord.Remarks}," +
            "CreateTime = #{alarmRecord.CreateTime}," +
            "GovernmentCode = #{alarmRecord.GovernmentCode},"+
            "Type = #{alarmRecord.Type} "+
            "DeviceName = #{alarmRecord.DeviceName} "+
            "Location = #{alarmRecord.Location} "+
            "where AlarmRecordCode=#{alarmRecord.AlarmRecordCode}")
    public int update(@Param("alarmRecord") AlarmRecord alarmRecord);

    @Select("select * from alarm_record  order by CreateTime desc")
    public List<AlarmRecord> getList();

    @Select("<script>" +
            "select * from alarm_record where GovernmentCode=#{governmentCode} " +
            "<if test ='type>0 '>" +
            " and Type=#{type} " +
            "</if>" +
            "<if test ='startTime!=null'>" +
            " and (CreateTime between #{startTime} and #{endTime})"+
            "</if>" +
            " order by CreateTime desc"+
            "</script> ")
    public List<AlarmRecordDTO> getListPage(@Param("type") Integer type,
                                            @Param("startTime") Date startTime,
                                            @Param("endTime") Date endTime,
                                            @Param("governmentCode") String governmentCode);

    @Select("select * from alarm_record where UserCode=#{userCode} order by CreateTime desc" )
    public List<AlarmRecordDTO> getListPageByUserCode(@Param("userCode") String userCode);

    @Select("select * from alarm_record where AlarmRecordCode=#{alarmRecordCode} limit 1")
    public AlarmRecord getByAlarmRecordCode(@Param("alarmRecordCode") String alarmRecordCode);

    @Select("select * from alarm_record where UserCode = #{userCode} order by CreateTime desc")
    public List<AlarmRecord> getListByUserCode(String userCode);

}
